package com.cy.pj.sys.service.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SysTimeAspect {
    @Pointcut("bean(sysLogServiceImpl)")
    public void doTime() {
    }

    @Before("doTime()")
    public void doBefore(JoinPoint jp) {
        System.out.println("@Before");
    }

    @After("doTime()")
    public void doAfter() {
        System.out.println("@After");
    }

    @AfterReturning("doTime()")
    public void doAfterReturning() {
        System.out.println("@AfterReturning");
    }

    @AfterThrowing("doTime()")
    public void doAfterThrowing() {
        System.out.println("@AfterThrowing");
    }

    //最重要,优先级也是最高
    @Around("doTime()")
    public Object doAround(ProceedingJoinPoint joinPoint)throws Throwable{
        System.out.println("Start:" + System.currentTimeMillis());
        try {
            System.out.println("@Around.before");
            Object result = joinPoint.proceed();
            System.out.println("@Around.afterReturning");
            return result;
        } catch (Throwable e) {
            System.out.println("@Around.afterThrowing");
            e.printStackTrace();
            throw e;
           /* try {
                return e.getMessage();//不可以这样写
            }catch (Throwable x){
                return new PageObject<>();
            }*/
            //return new PageObject<>();
            // return null;
        }finally {
            System.out.println("@Around.after");
        }

    }
}